package com.chat.datea.fragment;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.Build;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v7.graphics.Palette;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.BillFragment_RecyclerView_Adapter;
import com.chat.datea.base.BaseFragment;
import com.chat.datea.base.Finnal_Base;
import com.chat.datea.bean.BillList;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xyb on 15/10/23.
 */
public class BillFragment extends BaseFragment {
    private String billUrl;
    private VolleySingleton singleton;
    private RecyclerView recyclerView;
    private BillFragment_RecyclerView_Adapter recyclerViewAdapter;
    private List<BillList.SongListEntity> song_list;
    private ImageView singer_img;
    private Toolbar toolbar;
    private String pic_s192;
    private CollapsingToolbarLayout toolbarLayout;

    public BillFragment(int type, String pic_s192) {
        this.pic_s192 = pic_s192;
        singleton = VolleySingleton.getVolleySingleton(mActivity);
        billUrl = Finnal_Base.apiUrl + Finnal_Base.method + "baidu.ting.billboard.billList" + Finnal_Base.format + Finnal_Base.type + type;

        Log.e("BillFragment", billUrl);
    }

    @Override
    protected void initData() {

        StringRequest request = new StringRequest(billUrl, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                song_list = new ArrayList<>();
                BillList billList = gson.fromJson(response, BillList.class);
                Picasso.with(mActivity).load(pic_s192).into(singer_img);
                song_list = billList.getSong_list();
                recyclerViewAdapter = new BillFragment_RecyclerView_Adapter(mActivity, song_list, billList);
                initToolbarLayout(billList);


//                Palette palette = Palette.generate(bm);
//                toolbarLayout.setContentScrimColor(palette.getLightVibrantColor(Color.BLUE));
                recyclerView.setLayoutManager(new LinearLayoutManager(mActivity));
//                recyclerView.addItemDecoration(new DividerItemDecoration(mActivity, DividerItemDecoration.VERTICAL_LIST));
                recyclerView.setAdapter(recyclerViewAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(request, "BillFragment");
        toolbar.setNavigationIcon(R.mipmap.nav_back);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getActivity().getSupportFragmentManager().popBackStack();
            }
        });

    }

    private void initToolbarLayout(final BillList billList) {

        ImageRequest imageRequest = new ImageRequest(pic_s192, new Response.Listener<Bitmap>() {
            @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onResponse(Bitmap response) {
                singer_img.setImageBitmap(response);
                Palette palette = Palette.generate(response);
                int defaultColor = 0x99999999;
                int defaultTitleColor = Color.WHITE;
                int bgColor = palette.getDarkVibrantColor(defaultColor);
                int titleColor = palette.getLightVibrantColor(defaultTitleColor);
                toolbarLayout.setTitle(billList.getBillboard().getName());
                toolbarLayout.setCollapsedTitleTextColor(titleColor);
                toolbarLayout.setExpandedTitleColor(0x00000000);
                toolbarLayout.setContentScrimColor(bgColor);

            }
        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        singleton.addQueue(imageRequest, "BillFragment");
    }

    @Override
    protected void initView() {
        toolbar = (Toolbar) view.findViewById(R.id.toolbar);
        recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        singer_img = (ImageView) view.findViewById(R.id.singer_img);
        toolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsing_toolbar);
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container) {
        view = inflater.inflate(R.layout.fragment_bill, null);
        return view;
    }

}
